﻿Imports System.Text.RegularExpressions
Imports System.Text

Public Class WebPageRegex

    Implements IComparable

    '正则的层级
    Public Levels As Integer
    Public regAl As ArrayList
    '所有正则表达式进行匹配所得的结果
    ' Public mathString As String


    Dim titlearray As ArrayList
    Dim imageArray As ArrayList

    Private _regHash As Hashtable

    Public Sub New()
        regAl = New ArrayList()
        titlearray = New ArrayList()
        imageArray = New ArrayList()
        m_namedGroup = New Hashtable()
        '加入两个可以用的命名组
        AddNamedGroup("title")
        AddNamedGroup("imageurl")
    End Sub

    ''' <summary>
    ''' 添加正则表达式到集合中
    ''' </summary>
    ''' <param name="reg"></param>
    ''' <remarks></remarks>
    Public Sub AddRegexString(ByVal reg As String)
        If Not regAl.Contains(reg) Then
            regAl.Add(reg)
        End If
    End Sub
    ''' <summary>
    ''' 添加正则表达式,
    ''' </summary>
    ''' <param name="reg">正则表达式</param>
    ''' <param name="regname">要提取的名字</param>
    ''' <remarks></remarks>
    Public Sub AddRegexString(ByVal reg As String, ByVal regname As String)

    End Sub

    'Public ReadOnly Property ImageAL() As ArrayList
    '    Get
    '        Return Me.imageArray
    '    End Get
    'End Property

    'Public ReadOnly Property TitleAL() As ArrayList
    '    Get
    '        Return Me.titlearray
    '    End Get
    'End Property

    Public Property RegHash() As Hashtable
        Get
            Return _regHash
        End Get
        Set(ByVal value As Hashtable)
            _regHash = value
        End Set
    End Property


    Public Function MathString(ByRef regHash As Hashtable, ByVal content As String)
        Dim resSb As New StringBuilder()





        Return resSb.ToString
    End Function

    Private m_namedGroup As Hashtable

    ''' <summary>
    ''' 指定的分组名称
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property NamedGroup() As Hashtable
        Get
            Return m_namedGroup
        End Get
    End Property


    Public Sub AddNamedGroup(ByVal groupName As String)
        If (Not m_namedGroup.ContainsKey(groupName)) Then

            Dim target As New NamedGroupAndValue(groupName)
            m_namedGroup.Add(target.GroupName, target)
        End If
    End Sub

    Public Function GetNamedGroupString() As String
        Dim sb As New StringBuilder
        Dim nv As NamedGroupAndValue

        For Each entry As DictionaryEntry In m_namedGroup
            nv = TryCast(entry.Value, NamedGroupAndValue)
            sb.Append(entry.Key & "---" & nv.MatchValue & vbCrLf)
        Next

        Return sb.ToString
    End Function


    Public Function RegexMatchString(ByVal task As RegexTask, ByVal addsep As Boolean) As String
        Dim sep As String = vbCrLf & "--------------------------------------" & vbCrLf

        Dim source As String = task.WebString
        '存储文件名及图片名


        Dim mat_string As String = ""
        Try
            '把这个level的string来进行匹配
            For Each alregstr As String In regAl
                Dim reg As New Regex(alregstr)
                Dim matches As MatchCollection = reg.Matches(source)

                If addsep Then mat_string += task.WebUrl




                For Each mat As Match In matches
                    mat_string += mat.Value


                    Dim namedString As String = ""
                    'If alregstr.Contains("title") Then
                    '    Dim titleString As String = mat.Groups("title").Value
                    '    If Not String.IsNullOrEmpty(titleString) Then
                    '        namedString += vbCrLf & "title is:" & titleString & vbCrLf
                    '        titlearray.Add(titleString)
                    '    End If
                    'End If

                    For Each entry As DictionaryEntry In m_namedGroup
                        Dim nv As NamedGroupAndValue = TryCast(entry.Value, NamedGroupAndValue)

                        If (nv.IsMatched) Then
                            Continue For
                        End If

                        If alregstr.Contains(entry.Key) Then
                            Dim titleString As String = mat.Groups(entry.Key).Value
                            If Not String.IsNullOrEmpty(titleString) Then
                                nv.MatchValue = titleString
                            End If
                        End If
                    Next

                    'If alregstr.Contains("imageurl") Then

                    '    Dim imageurl As String

                    '    imageurl = mat.Groups("imageurl").Value
                    '    If Not String.IsNullOrEmpty(imageurl) Then
                    '        namedString += vbCrLf & "image url is:" & imageurl & vbCrLf
                    '        imageArray.Add(imageurl)
                    '    End If

                    'End If
                    mat_string += namedString
                    If addsep Then mat_string += sep

                Next


            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Return mat_string
    End Function






    Public Overrides Function ToString() As String
        Dim str As String = ""


        For Each regstr As String In regAl
            str += Levels.ToString & "|" & regstr & "   "
        Next


        Return str
    End Function


    Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo

        If obj Is Nothing Then Return 1
        Dim _regex As WebPageRegex = TryCast(obj, WebPageRegex)



        If _regex IsNot Nothing Then
            Return Me.Levels.CompareTo(_regex.Levels)
        Else
            Throw New ArgumentException("Object is not a WebPageRegex")
        End If

    End Function
End Class


''' <summary>
''' 需要匹配的命名组与匹配的值
''' </summary>
''' <remarks></remarks>
Public Class NamedGroupAndValue

    Private m_groupname As String
    Private m_matchvalue As String
    Private m_ismatched As Boolean


    Public Sub New(ByVal groupName As String)
        m_groupname = groupName
    End Sub


    Public ReadOnly Property GroupName() As String
        Get
            Return m_groupname
        End Get
    End Property


    Public Property MatchValue() As String
        Get
            Return m_matchvalue
        End Get
        Set(ByVal value As String)
            If (Not String.IsNullOrEmpty(value)) Then
                m_matchvalue = value
            End If
        End Set
    End Property

    Public ReadOnly Property IsMatched() As Boolean
        Get
            Return m_ismatched
        End Get
    End Property
End Class